package com.situ.mvc.controller.text;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping(value="/fileupload")
public class FileUploadController {
	
	//日常进入from表单
	@RequestMapping(value="/file")
	public String file() {
		return "fileupload";
	}
	
	@RequestMapping(value="/upload")
	//MultipartFilter是封装的上传信息(文件名、路径、大小)
	public void upload(String name, MultipartFile multipartFile) {
		System.out.println(name);
		System.out.println(multipartFile.getContentType());//上传的文件类型/后缀格式   /image/png
		System.out.println(multipartFile.getName()); //multipartFile
		System.out.println(multipartFile.getOriginalFilename());//上传的完整文件名    /百度换肤.png
		System.out.println(multipartFile.getSize());//文件大小   	`			   /1755349
		
		//定义输入输出流
		InputStream inputStream = null;
		OutputStream outputStream = null;
		
		try {
			inputStream = multipartFile.getInputStream(); //写入文件
			//写出文件，文件名为原先的文件名
			outputStream  = new FileOutputStream(new File("D:/" + multipartFile.getOriginalFilename()));
			IOUtils.copy(inputStream, outputStream);  //执行copy操作
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			IOUtils.closeQuietly(outputStream);
			IOUtils.closeQuietly(inputStream);
		}
		
	}
}
